package com.yhsimon.singleton.type2;

public class SingletonTest2 {
    public static void main(String[] args) {
        //饿汉式（静态变量）测试
        Singleton i1 = Singleton.getInstance();
        Singleton i2 = Singleton.getInstance();
        System.out.println("i1与i2实例是否相等："+(i1==i2));
        System.out.println("i1与i2的哈希值是否相同：i1的哈希值为 "+i1.hashCode()+"i2的哈希值为："+i2.hashCode()+"---"+(i1.hashCode()==i2.hashCode()));

    }

}

//饿汉式(静态代码块)
class Singleton{
    //1.构造器私有化，外部不能new
    private Singleton(){
    }
    //2.本类内部创建对象实例
    private  static Singleton instance;

    static {
        instance=new Singleton();
    }

    //3.提供一个公有的静态方法，返回实例对象
    public static Singleton getInstance(){
        return instance;
    }
}